window.onload = async function () {
  const { data: res } = await axios({
    url: 'http://ajax-api.itheima.net/dashboard',
    method: 'get',

    headers: {
      Authorization: localStorage.getItem('token'),
    },
  })
  // console.log('res:', res)
  const {
    data: { overview },
  } = res
  for (const key in overview) {
    document.querySelector(`span[name=${key}]`).innerHTML = overview[key]
  }
  fn(res)
  classlist(res)
  console.log(res)
  teamList(res)
  sexList(res)
  initMapChart(res.data.provinceData)
}
// 曲线图
const fn = (res) => {
  const line = document.querySelector('#line')
  let myChart = echarts.init(line)
  let option

  option = {
    title: { text: '22年工资图', left: '2%', top: '4%' },
    tooltip: {
      trigger: 'axis',
    },
    //设置绘图网格
    grid: {
      //网格距离上方距离
      top: '20%',
    },

    //设置折线图为渐变颜色
    color: [
      {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 1,
        y2: 0,
        colorStops: [
          {
            offset: 0,
            color: '#499FEE', // 0% 处的颜色
          },
          {
            offset: 1,
            color: '#5D75F0', // 100% 处的颜色
          },
        ],
      },
    ],
    xAxis: {
      type: 'category',
      // data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
      data: res.data.year.map((item) => item.month),
      //设置坐标轴轴线样式（颜色和虚线）
      axisLine: {
        lineStyle: {
          color: '#ccc',
          type: 'dashed',
        },
      },
      //坐标轴标签的颜色
      axisLabel: {
        color: '#999',
      },
    },
    yAxis: {
      type: 'value',
      //分隔线的样式
      splitLine: {
        lineStyle: {
          type: 'dashed',
        },
      },
    },
    series: [
      {
        data: res.data.year.map((item) => item.salary),
        type: 'line',
        smooth: true,
        symbolSize: 10,
        lineStyle: {
          width: 6,
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: '#499FEE',
            },
            {
              offset: 0.8,
              color: 'rgba(255,255,255,0.2)',
            },
            {
              offset: 1,
              color: 'rgba(255,255,255,0)',
            },
          ]),
        },
      },
    ],
  }
  option && myChart.setOption(option)
}
// 班级饼图
const classlist = (res) => {
  const line = document.querySelector('#salary')
  let myChart = echarts.init(line)
  let option

  option = {
    title: { text: '班级薪资分布', left: '2%', top: '4%' },
    color: ['#FDA224', '#5097FF', '#3ABCFA', '#34D39A'],
    tooltip: {
      trigger: 'item',
    },
    legend: {
      bottom: '5%',
      left: 'center',
    },
    series: [
      {
        name: '班级薪资分布',
        type: 'pie',
        radius: ['50%', '64%'],
        center: ['50%', '45%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2,
        },
        label: {
          show: false,
          position: 'center',
        },

        labelLine: {
          show: false,
        },
        data: res.data.salaryData.map((item) => {
          return { name: item.label, value: item.b_count + item.g_count }
        }),
      },
    ],
  }
  option && myChart.setOption(option)
}
const button = document.querySelector('#btns')
// 柱状图
const teamList = (res) => {
  const line = document.querySelector('#lines')
  let myChart = echarts.init(line)
  let option

  option = {
    grid: {
      left: 70,
      top: 30,
      right: 30,
      bottom: 50,
    },
    xAxis: {
      type: 'category',
      data: res.data.groupData[1].map((item) => item.name),
      axisLine: {
        lineStyle: {
          color: '#ccc',
          type: 'dashed',
        },
      },
      axisLabel: {
        color: '#999',
      },
    },
    yAxis: {
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed',
        },
      },
    },
    color: [
      {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
          {
            offset: 0,
            color: '#34D39A', // 0% 处的颜色
          },
          {
            offset: 1,
            color: 'rgba(52,211,154,0.2)', // 100% 处的颜色
          },
        ],
      },
      {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
          {
            offset: 0,
            color: '#499FEE', // 0% 处的颜色
          },
          {
            offset: 1,
            color: 'rgba(73,159,238,0.2)', // 100% 处的颜色
          },
        ],
      },
    ],
    series: [
      {
        data: res.data.groupData[1].map((item) => item.hope_salary),
        type: 'bar',
        name: '期望薪资',
      },
      {
        data: res.data.groupData[1].map((item) => item.salary),
        type: 'bar',
        name: '就业薪资',
      },
    ],
  }
  option && myChart.setOption(option)

  button.addEventListener('click', function (e) {
    if (e.target.tagName === 'BUTTON') {
      const num = e.target.innerHTML
      option = {
        xAxis: {
          type: 'category',
          data: res.data.groupData[num].map((item) => item.name),
          axisLine: {
            lineStyle: {
              color: '#ccc',
              type: 'dashed',
            },
          },
        },
        series: [
          {
            data: res.data.groupData[num].map((item) => item.hope_salary),
            type: 'bar',
            name: '期望薪资',
          },
          {
            data: res.data.groupData[num].map((item) => item.salary),
            type: 'bar',
            name: '就业薪资',
          },
        ],
      }
      option && myChart.setOption(option)
      button.querySelector('.btn-blue').classList.remove('btn-blue')
      e.target.classList.add('btn-blue')
    }
  })
}
// 中国地图
const initMapChart = (provinceData) => {
  const myEchart = echarts.init(document.querySelector('#map'))
  const dataList = [
    { name: '南海诸岛', value: 0 },
    { name: '北京', value: 0 },
    { name: '天津', value: 0 },
    { name: '上海', value: 0 },
    { name: '重庆', value: 0 },
    { name: '河北', value: 0 },
    { name: '河南', value: 0 },
    { name: '云南', value: 0 },
    { name: '辽宁', value: 0 },
    { name: '黑龙江', value: 0 },
    { name: '湖南', value: 0 },
    { name: '安徽', value: 0 },
    { name: '山东', value: 0 },
    { name: '新疆', value: 0 },
    { name: '江苏', value: 0 },
    { name: '浙江', value: 0 },
    { name: '江西', value: 0 },
    { name: '湖北', value: 0 },
    { name: '广西', value: 0 },
    { name: '甘肃', value: 0 },
    { name: '山西', value: 0 },
    { name: '内蒙古', value: 0 },
    { name: '陕西', value: 0 },
    { name: '吉林', value: 0 },
    { name: '福建', value: 0 },
    { name: '贵州', value: 0 },
    { name: '广东', value: 0 },
    { name: '青海', value: 0 },
    { name: '西藏', value: 0 },
    { name: '四川', value: 0 },
    { name: '宁夏', value: 0 },
    { name: '海南', value: 0 },
    { name: '台湾', value: 0 },
    { name: '香港', value: 0 },
    { name: '澳门', value: 0 },
  ]

  const list = provinceData.map((item) => {
    return { name: item.name.replace(/省/, ''), value: item.value }
  })
  list.forEach((item, index, arr) => {
    if (arr[index].name === '西藏自治区') {
      arr[index].name = '西藏'
    }
    if (arr[index].name === '内蒙古自治区') {
      arr[index].name = '内蒙古'
    }
    if (arr[index].name === '新疆维吾尔自治区') {
      arr[index].name = '新疆'
    }
    if (arr[index].name === '宁夏回族自治区') {
      arr[index].name = '宁夏'
    }
    if (arr[index].name === '广西壮族自治区') {
      arr[index].name = '广西'
    }
  })
  for (let i = 0; i < dataList.length; i++) {
    for (let j = 0; j < list.length; j++) {
      if (dataList[i].name === list[j].name) {
        dataList[i].value = list[j].value
      }
    }
  }
  // for (const key in dataList) {
  //   list[key].name? (dataList[key].name===list[key].name?dataList[key].value=list[key].value:dataList):''
  //   }
  // console.log(dataList)
  let option = {
    title: {
      text: '籍贯分布',
      top: 10,
      left: 10,
      textStyle: {
        fontSize: 16,
      },
    },
    tooltip: {
      trigger: 'item',
      formatter: '{b}: {c} 位学员',
      borderColor: 'transparent',
      backgroundColor: 'rgba(0,0,0,0.5)',
      textStyle: {
        color: '#fff',
      },
    },
    visualMap: {
      min: 0,
      max: 6,
      left: 'left',
      bottom: '20',
      text: ['6', '0'],
      inRange: {
        color: ['#ffffff', '#0075F0'],
      },
      show: true,
      left: 40,
    },
    geo: {
      map: 'china',
      roam: false,
      zoom: 1.0,
      label: {
        normal: {
          show: true,
          fontSize: '10',
          color: 'rgba(0,0,0,0.7)',
        },
      },
      itemStyle: {
        normal: {
          borderColor: 'rgba(0, 0, 0, 0.2)',
          color: '#e0ffff',
        },
        emphasis: {
          areaColor: '#34D39A',
          shadowOffsetX: 0,
          shadowOffsetY: 0,
          shadowBlur: 20,
          borderWidth: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)',
        },
      },
    },
    series: [
      {
        name: '籍贯分布',
        type: 'map',
        geoIndex: 0,
        data: dataList,
      },
    ],
  }
  myEchart.setOption(option)
}
// 性别饼图
const sexList = (res) => {
  const line = document.querySelector('#gender')
  let myChart = echarts.init(line)
  let option
  const data = res.data.salaryData.map((item) => {
    return {
      value: item.g_count,
      name: item.label,
    }
  })
  option = {
    title: [
      {
        text: '男女薪资图分布',
      },
      {
        text: '男',
        top:"50%",
        left: "45%",
      
      },
      {
        text: '女',
        bottom:'0',
        left: "45%",
 
        subtextStyle: {
        } 
      },
    ],

    tooltip: {
      trigger: 'item',
    },
    series: [
      {
        type: 'pie',
        radius: ['30%', '45%'],
        height: '50%',
        top: '5%',
        avoidLabelOverlap: false,
        itemStyle: {},
        label: {
          show: true,
          position: 'outside',
        },

        labelLine: {
          show: true,
        },
        data: res.data.salaryData.map((item) => {
          return {
            value: item.g_count,
            name: item.label,
          }
        }),
      },
      {
        type: 'pie',
        radius: ['30%', '45%'],
        height: '50%',
        top: '50%',
        avoidLabelOverlap: false,
        itemStyle: {},
        label: {
          show: true,
          position: 'outside',
        },

        labelLine: {
          show: true,
        },
        data: res.data.salaryData.map((item) => {
          return {
            value: item.b_count,
            name: item.label,
          }
        }),
      },
    ],
  }
  option && myChart.setOption(option)
}
